import java.util.*;

/**
 * @author hewei
 * @version 1.0
 * @description: 1129. 颜色交替的最短路径
 * @date 2022/11/18 15:18
 */

public class ShortestAlternatingPaths {

    public int[] shortestAlternatingPaths1(int n, int[][] redEdges, int[][] blueEdges) {
        Deque<Integer> queue = new ArrayDeque<>();
        boolean[] flag1 = new boolean[n];
        boolean[] flag2 = new boolean[n];
        HashMap<Integer, List<Integer>> redMap = new HashMap<>();
        HashMap<Integer, List<Integer>> blueMap = new HashMap<>();
        process(redEdges, redMap);
        process(blueEdges, blueMap);
        queue.add(0);
        flag1[0] = true;
        int[] ans = new int[n];
        Arrays.fill(ans, -1);
        ans[0] = 0;
        bfs(queue, ans, true, redMap, flag1, blueMap, flag2);
        flag1 = new boolean[n];
        flag2 = new boolean[n];
        queue.add(0);
        flag2[0] = true;
        bfs(queue, ans, false, redMap, flag1, blueMap, flag2);
        return ans;
    }

    private void bfs(Deque<Integer> queue, int[] ans, boolean color, HashMap<Integer, List<Integer>> redMap, boolean[] flag1, HashMap<Integer, List<Integer>> blueMap, boolean[] flag2) {
        int step = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            while (size-- > 0) {
                Integer poll = queue.poll();
                if (ans[poll] == -1) {
                    ans[poll] = step;
                } else {
                    ans[poll] = Math.min(ans[poll], step);
                }
                if (color) {
                    List<Integer> list = redMap.get(poll);
                    if (list != null) {
                        for (Integer index : list) {
                            if (!flag1[index]) {
                                queue.add(index);
                                flag1[index] = true;
                            }
                        }
                    }
                } else {
                    List<Integer> list = blueMap.get(poll);
                    if (list != null) {
                        for (Integer index : list) {
                            if (!flag2[index]) {
                                queue.add(index);
                                flag2[index] = true;
                            }
                        }
                    }
                }
            }
            ++step;
            color = !color;
        }
    }

    public void process(int[][] edges, HashMap<Integer, List<Integer>> map) {
        for (int[] edge : edges) {
            if (!map.containsKey(edge[0])) {
                map.put(edge[0], new ArrayList<>());
            }
            map.get(edge[0]).add(edge[1]);
        }
    }

    public int[] shortestAlternatingPaths(int n, int[][] redEdges, int[][] blueEdges) {
        List<Integer>[] redList = new ArrayList[n];
        List<Integer>[] blueList = new ArrayList[n];
        HashMap<Integer, List<Integer>[]> map = new HashMap<>(2);
        map.put(0, redList);
        map.put(1, blueList);
        for (int i = 0; i < n; i++) {
            redList[i] = new ArrayList<>();
            blueList[i]= new ArrayList<>();
        }
        for (int[] edge : blueEdges) {
            blueList[edge[0]].add(edge[1]);
        }
        for (int[] edge : redEdges) {
            redList[edge[0]].add(edge[1]);
        }
        Deque<int[]> queue = new ArrayDeque<>();
        boolean[][] flag = new boolean[n][2];
        int[] ans = new int[n];
        Arrays.fill(ans, -1);
        queue.add(new int[]{0, 0});
        queue.add(new int[]{0, 1});
        flag[0][0] = true;
        flag[0][1] = true;
        int step = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            while (size-- > 0) {
                int[] poll = queue.poll();
                if (ans[poll[0]] == -1) {
                    ans[poll[0]] = step;
                } else {
                    ans[poll[0]] = Math.min(ans[poll[0]], step);
                }
                for (Integer index : map.get(poll[1])[poll[0]]) {
                    if (!flag[index][1 ^ poll[1]]) {
                        queue.add(new int[]{index, 1 ^ poll[1]});
                        flag[index][1 ^ poll[1]] = true;
                    }
                }
            }
            ++step;
        }
        return ans;
    }

}
